package com.customview.wzq.recycleviewdemo;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * 创建时间：2017/7/12
 * 作者：kb_jay
 * 功能描述：
 */

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerHolder> {

    private List<MyData> mData;
    private CustomListener customListener;

    public void setCustomListener(CustomListener listener) {
        this.customListener = listener;
    }
    public RecyclerAdapter(List<MyData> data) {
        mData = data;
    }

    @Override
    public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View child = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv, parent, false);
        return new RecyclerHolder(child);
    }

    @Override
    public void onBindViewHolder(RecyclerHolder holder, final int position) {

        holder.tvItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(customListener!=null){
                    customListener.onItemClick(position);
                }
            }
        });
        holder.tvItem.setText(String.valueOf(mData.get(position).number));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class RecyclerHolder extends RecyclerView.ViewHolder {

        public TextView tvItem;

        public RecyclerHolder(View itemView) {
            super(itemView);
            tvItem = (TextView) itemView.findViewById(R.id.tv_item);
        }
    }

    public interface CustomListener {
        void onItemClick(int position);
    }
}
